<template>
  <list-card show-asider :collapse.sync="collapse">
    <template v-slot:aside>
      <dept-tree-card ref="tree" @selected="handleSeleteDept" style="vertical-align:top"/>
    </template>
    <template v-slot:search>
      <el-form ref="queryForm" inline :model="queryParams" size="small">
        <el-form-item label="用户姓名" prop="name">
          <el-input v-model="queryParams.name" clearable placeholder="请输入用户姓名"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item v-if="showSearch" label="用户账号" prop="code">
          <el-input v-model="queryParams.code" clearable placeholder="请输入用户账号"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item v-if="showSearch" label="手机号码" prop="mobile">
          <el-input v-model="queryParams.mobile" clearable placeholder="请输入手机号码"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item v-if="showSearch" label="性别" prop="sex">
          <el-select v-model="queryParams.sex" clearable placeholder="请选择用户性别">
            <el-option v-for="item in localDict.user_sex" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item v-if="showSearch" label="用户类型" prop="type">
          <el-select v-model="queryParams.type" clearable placeholder="请选择用户类型">
            <el-option v-for="item in localDict.user_type" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item v-if="showSearch" label="用户状态" prop="status">
          <el-select v-model="queryParams.status" clearable placeholder="请选择用户状态">
            <el-option v-for="item in localDict.user_status" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </template>
    <template v-slot:operate>
      <el-button v-has-permi="['system:user:add']" icon="el-icon-plus" plain size="mini" type="primary"
                 @click="handleAdd">新增
      </el-button>
      <el-button v-has-permi="['system:user:remove']" :disabled="multiple" icon="el-icon-delete" plain size="mini"
                 type="danger" @click="handleDelete">删除
      </el-button>
      <el-button v-has-permi="['system:user:resetPwd']" :disabled="multiple" icon="el-icon-key" plain size="mini"
                 type="warning" @click="handleResetPwd">重置密码
      </el-button>
      <!--      <el-button v-has-permi="['system:user:remove']" icon="el-icon-data-board" plain size="mini"-->
      <!--                 @click="handleRecycle">-->
      <!--        回收站-->
      <!--      </el-button>-->
      <!--      <el-button v-has-permi="['system:user:import']" icon="el-icon-upload2" plain size="mini" type="success"-->
      <!--                 @click="handleImport">导入-->
      <!--      </el-button>-->
      <!--      <el-button v-has-permi="['system:user:export']" icon="el-icon-download" plain size="mini" type="info"-->
      <!--                 @click="handleExport">导出-->
      <!--      </el-button>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"/>
    </template>
    <template v-slot:content>
      <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="50"/>
        <el-table-column align="center" label="用户账号" prop="code" width="120"/>
        <el-table-column align="center" label="用户姓名" prop="name" width="120" show-overflow-tooltip/>
        <el-table-column align="center" label="用户类型" prop="type" width="150">
          <template v-slot:default="scope">
            <dict-tag :options="localDict.user_type" :value="scope.row.type" show-label/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="性别" prop="sex" width="120">
          <template v-slot:default="scope">
            <dict-tag :options="localDict.user_sex" :value="scope.row.sex" show-label/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="手机号码" prop="mobile" width="120"/>
        <el-table-column align="center" label="电子邮箱" prop="email" width="120" show-overflow-tooltip/>
        <el-table-column align="center" label="状态" prop="status" width="120">
          <template v-slot:default="scope">
            <dict-tag :options="localDict.user_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <!--        <el-table-column align="center" label="注册时间" prop="createTime" width="160"/>-->
        <el-table-column align="center" fixed="right" label="操作" width="200">
          <template v-slot:default="scope">
            <el-button v-has-permi="['system:user:detail']" icon="el-icon-tickets" size="mini" type="text"
                       @click="handleDetail(scope.row)">详情
            </el-button>
            <el-button v-has-permi="['system:user:remove']" icon="el-icon-delete" size="mini" type="text"
                       @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="pageParam.total>0" :total="pageParam.total" :limit.sync="pageParam.pageSize"
                  :page.sync="pageParam.pageNum" @pagination="getList"/>
    </template>
    <model-card ref="model" @ok="getList"/>
    <recycle-card ref="recycle" :base-url="baseUrl" :columns="columns" :dicts="localDict" @ok="getList"/>
    <import-card ref="import" :base-url="baseUrl" bus-name="用户数据" @ok="getList"/>
  </list-card>
</template>

<script>
import moment from "moment";
import {list, remove} from "@/api/base";
import ModelCard from "./components/ModelCard.vue";
import DeptTreeCard from "../dept/components/TreeCard.vue";
import listMixin from "../../../mixins/listMixin";
import {resetPwd} from "../../../api/profile";

export default {
  name: "User",
  mixins: [listMixin],
  components: {DeptTreeCard, ModelCard},
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      collapse: false,
      baseUrl: '/system/user',
      // 遮罩层
      loading: true,
      // 用户表格数据
      dataList: [],
      pageParam: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      // 查询参数
      queryParams: {
        params: {
          createTime: undefined
        }
      },
      // 显示搜索条件
      showSearch: false,
      localDict: {
        user_sex: [],
        user_type: [],
        user_status: [],
      },
      // 列信息
      columns: [
        {label: '用户编号', props: 'code', dict: false, visible: true, width: 100},
        {label: '用户名称', props: 'name', dict: false, visible: true, width: 100},
        {label: '用户类型', props: 'type', dict: true, code: 'user_type', tag: true, visible: true, width: 140},
        {label: '手机号码', props: 'mobile', visible: true, width: 150},
        {label: '电子邮箱', props: 'email', visible: true},
      ],
    };
  },
  created() {
    this.getLocalDict();
    this.resetQuery()
  },
  methods: {
    async getLocalDict() {
      await this.getStaticData('system-user-sex').then(res => {
        this.localDict.user_sex = res.data
      })
      await this.getStaticData('system-user-type').then(res => {
        this.localDict.user_type = res.data
      })
      await this.getStaticData('system-status').then(res => {
        this.localDict.user_status = res.data
      })
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      list(this.baseUrl, {
        ...this.queryParams,
        pageNum: this.pageParam.pageNum,
        pageSize: this.pageParam.pageSize
      }).then(response => {
          this.dataList = response.rows;
          this.pageParam.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        params: {
          createTime: undefined,
        }
      }
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSeleteDept(dept) {
      this.queryParams.deptId = dept.id
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.model.add()
    },
    /** 修改按钮操作 */
    handleEdit(row) {
      this.$refs.model.edit(row.id)
    },
    /** 重置密码按钮操作 */
    handleResetPwd() {
      this.$modal.confirm('是否确认将选择的用户登陆密码重置为初始密码?').then(() => {
        resetPwd(this.ids).then(() => {
          this.getList();
          this.$modal.msgSuccess("密码重置成功");
        }).catch(() => {
        });
      });
    },
    handleDetail(row) {
      console.log("selected row:=====", row)
      this.$router.push({name: 'UserDetail', path: '/system/user/detail/' + row.id, params: {id: row.id}});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选择的用户？').then(() => {
        remove(this.baseUrl, ids, true).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {

        });
      });
    },
    handleRecycle() {
      this.$refs.recycle.visible = true
      this.$refs.recycle.title = "用户管理回收站"
      this.$refs.recycle.resetQuery()
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(`${this.baseUrl}/export`, {...this.queryParams}, `用户数据_${moment().format('YYYYMMDD')}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.$refs.import.visible = true
    },
  }
};
</script>
